package com.xindaoapp.happypet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.ConstantsValue;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.bean.PostLandlordInfo;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.ProgressHUD;

public class IwantJoinActivity extends UMBaseActivity implements OnClickListener {
    private PostLandlordInfo info;
    private LinearLayout ll_name;
    private LinearLayout ll_tel;
    private LinearLayout ll_mode;
    private RelativeLayout rl_word;
    private RelativeLayout rl_word2;
    private LinearLayout ll_qq;
    private LinearLayout ll_sex;

    private EditText et_name;
    private EditText et_tel;
    private EditText et_mode;
    private EditText et_qq;
    private EditText et_word;
    private EditText et_sex;
    private EditText et_word2;


    private View tittle_common_cancel;
    private TextView submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iwantjoin);
        info = (PostLandlordInfo) getIntent().getExtras().getSerializable("info");
        initView();
    }

    String s = "";

    private void initView() {
        ll_name = (LinearLayout) findViewById(R.id.ll_name);
        ll_tel = (LinearLayout) findViewById(R.id.ll_tel);
        ll_mode = (LinearLayout) findViewById(R.id.ll_mode);
        rl_word = (RelativeLayout) findViewById(R.id.rl_word);
        rl_word2 = (RelativeLayout) findViewById(R.id.rl_word2);
        ll_qq = (LinearLayout) findViewById(R.id.ll_qq);
        ll_sex = (LinearLayout) findViewById(R.id.ll_sex);

        et_word2 = (EditText) findViewById(R.id.et_word2);
        et_name = (EditText) findViewById(R.id.et_name);
        et_tel = (EditText) findViewById(R.id.et_tel);
        et_mode = (EditText) findViewById(R.id.et_mode);
        et_qq = (EditText) findViewById(R.id.et_qq);
        et_word = (EditText) findViewById(R.id.et_word);
        et_sex = (EditText) findViewById(R.id.et_sex);

        tittle_common_cancel = findViewById(R.id.tittle_common_cancel);
        submit = (TextView) findViewById(R.id.submit);

        tittle_common_cancel.setOnClickListener(this);
        submit.setOnClickListener(this);


        String[] userfield = info.activityinfo.ufield.userfield;
        if (userfield != null) {
            for (String string : userfield) {
                s = s + string;
            }
        }
        if (!TextUtils.isEmpty(s)) {
            if (s.contains("realname")) {
                ll_name.setVisibility(View.VISIBLE);
            } else {
                ll_name.setVisibility(View.GONE);
            }
            if (s.contains("mobile")) {
                ll_tel.setVisibility(View.VISIBLE);
            } else {
                ll_tel.setVisibility(View.GONE);
            }
            if (s.contains("gender")) {
                ll_sex.setVisibility(View.VISIBLE);
            } else {
                ll_sex.setVisibility(View.GONE);
            }
            if (s.contains("address")) {
                ll_mode.setVisibility(View.VISIBLE);
            } else {
                ll_mode.setVisibility(View.GONE);
            }
            if (s.contains("qq")) {
                ll_qq.setVisibility(View.VISIBLE);
            } else {
                ll_qq.setVisibility(View.GONE);
            }
            rl_word2.setVisibility(View.GONE);
        } else {
            ll_name.setVisibility(View.GONE);
            ll_sex.setVisibility(View.GONE);
            ll_tel.setVisibility(View.GONE);
            ll_mode.setVisibility(View.GONE);
            ll_qq.setVisibility(View.GONE);
            rl_word.setVisibility(View.GONE);
            rl_word2.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_common_cancel:
                finish();
                break;
            case R.id.submit:
                if (UserState.isLogged()) {
                    name = et_name.getText().toString();
                    sex = et_sex.getText().toString();
                    tel = et_tel.getText().toString();
                    address = et_mode.getText().toString();
                    word = et_word.getText().toString();
                    qq = et_qq.getText().toString();
                    word2 = et_word2.getText().toString();
                    if (TextUtils.isEmpty(word) && TextUtils.isEmpty(word2)) {
                        Toast.makeText(IwantJoinActivity.this, "请填写留言", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (s.contains("gender")) {
                        if (sex.equals("男") || sex.equals("女")) {
                            Toast.makeText(getApplicationContext(), "请输入正确的性别", Toast.LENGTH_SHORT)
                                    .show();
                            return;
                        }
                    }
                    joinActivity();
                }
                break;
            default:
                break;
        }
    }

    private String name;
    private String sex;
    private String tel;
    private String address;
    private String qq;
    private String word;
    private String word2;

    private void joinActivity() {
        if (CommonUtil.isNetworkAvailable(getApplicationContext()) != 0) {
            RequestParams params = new RequestParams();
            params.put("realname", name);
            params.put("tid", info.tid);
            params.put("qq", qq);
            params.put("address", address);
            params.put("mobile", tel);
            if (sex.equals("男")) {
                params.put("gender", "1");
            } else if (sex.equals("女")) {
                params.put("gender", "2");
            }
            if (rl_word.getVisibility() == View.VISIBLE) {
                params.put("message", word);
            } else if (rl_word2.getVisibility() == View.VISIBLE) {
                params.put("message", word2);
            }
            final ProgressHUD ProgressDialog =
                    ProgressHUD.show(IwantJoinActivity.this, "正在加载...", true, true, null);
            AddClient
                    .get("http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=activity_apply",
                            params, new AsyncHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, String content) {
                                    try {
                                        BaseEntity parse =
                                                JSON.parseObject(content, BaseEntity.class);
                                        if (parse.getResult().equals("0")) {
                                            Toast.makeText(getApplicationContext(), parse.getMsg(),
                                                    Toast.LENGTH_SHORT).show();
                                            sendBroadcast(new Intent(
                                                    ConstantsValue.JOIN_ACTIVITY_SUCCESS));
                                            finish();
                                        } else if (parse.getResult().equals("-2")) {
                                            Toast.makeText(getApplicationContext(), parse.getMsg(),
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (Exception e) {

                                    }
                                    super.onSuccess(statusCode, content);
                                }

                                @Override
                                public void onFinish() {
                                    if (null != ProgressDialog && ProgressDialog.isShowing()) {
                                        ProgressDialog.dismiss();
                                    }
                                    super.onFinish();
                                }

                                @Override
                                public void onFailure(Throwable error, String content) {
                                    Toast.makeText(IwantJoinActivity.this, "提交失败",
                                            Toast.LENGTH_SHORT).show();
                                    super.onFailure(error, content);
                                }
                            });
        } else {
            Toast.makeText(getApplicationContext(), R.string.pLease_check_network,
                    Toast.LENGTH_SHORT).show();
            return;
        }
    }

}
